home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / term / term41source.lha / Extras / Source / term-Source.lha / termSendText.c < prev    next >
C/C++ Source or Header  |  1994-08-26  |  8KB  |  548 lines

  1. /*
  2. **    termSendText.c
  3. **
  4. **    Text sending support routines
  5. **
  6. **    Copyright © 1990-1994 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termARexxGlobal.h"
  11.  
  12.     /* Local variables. */
  13.  
  14. STATIC LONG WaitCount,PromptCount;
  15.  
  16.     /* MatchPrompt():
  17.      *
  18.      *    Search incoming data stream for a match.
  19.      */
  20.  
  21. STATIC BYTE __regargs
  22. MatchPrompt(register STRPTR Data,register LONG Size,register STRPTR Prompt,register LONG PromptLen)
  23. {
  24.     register UBYTE c,Mask;
  25.  
  26.     if(Config -> SerialConfig -> StripBit8)
  27.         Mask = 0x7F;
  28.     else
  29.         Mask = 0xFF;
  30.  
  31.     do
  32.     {
  33.         if(c = ((*Data++) & Mask))
  34.         {
  35.             register BYTE MatchMade;
  36.  
  37.             do
  38.             {
  39.                 MatchMade = FALSE;
  40.  
  41.                 if(PromptCount == WaitCount)
  42.                 {
  43.                     if(c == Prompt[WaitCount] & Mask)
  44.                     {
  45.                         MatchMade = TRUE;
  46.  
  47.                         if(PromptLen == ++PromptCount)
  48.                             return(TRUE);
  49.                     }
  50.                 }
  51.  
  52.                 if(MatchMade)
  53.                     WaitCount++;
  54.                 else
  55.                 {
  56.                     if(WaitCount)
  57.                     {
  58.                         WaitCount = 0;
  59.  
  60.                         PromptCount = 0;
  61.                     }
  62.                     else
  63.                         break;
  64.                 }
  65.             }
  66.             while(!WaitCount);
  67.         }
  68.     }
  69.     while(--Size);
  70.  
  71.     return(FALSE);
  72. }
  73.  
  74.     /* WaitForPrompt(STRPTR Prompt,LONG PromptLen):
  75.      *
  76.      *    Scan the incoming data flow for a certain string.
  77.      */
  78.  
  79. STATIC BYTE __regargs
  80. WaitForPrompt(STRPTR Prompt,LONG PromptLen)
  81. {
  82.     ULONG Signals;
  83.  
  84.     WaitCount = PromptCount = 0;
  85.  
  86.     if(DataHold)
  87.     {
  88.         DataHold = NULL;
  89.  
  90.         RestartSerial(FALSE);
  91.     }
  92.  
  93.     if(CheckSerialRead())
  94.         Signals = SIG_SERIAL;
  95.     else
  96.         Signals = NULL;
  97.  
  98.     StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  99.  
  100.     for(;;)
  101.     {
  102.         if(Signals & SIG_SERIAL)
  103.         {
  104.             if(!WaitSerialRead())
  105.             {
  106.                 LONG Length;
  107.  
  108.                 BytesIn++;
  109.  
  110.                 if(Translate_CR_LF)
  111.                     Length = (* Translate_CR_LF)(ReadBuffer,1);
  112.                 else
  113.                     Length = 1;
  114.  
  115.                 if(Length)
  116.                 {
  117.                     ConProcess(ReadBuffer,Length);
  118.  
  119.                     Status = STATUS_READY;
  120.  
  121.                     if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
  122.                     {
  123.                         if(!CheckIO(TimeRequest))
  124.                             AbortIO(TimeRequest);
  125.  
  126.                         WaitIO(TimeRequest);
  127.  
  128.                         RestartSerial(FALSE);
  129.  
  130.                         return(TRUE);
  131.                     }
  132.                 }
  133.  
  134.                 do
  135.                 {
  136.                         /* Check how many bytes are still in
  137.                          * the serial buffer.
  138.                          */
  139.  
  140.                     if(Length = GetSerialWaiting())
  141.                     {
  142.                         if(Length > SerialBufferSize)
  143.                             Length = SerialBufferSize;
  144.  
  145.                         if(!DoSerialRead(ReadBuffer,Length))
  146.                         {
  147.                             BytesIn += Length;
  148.  
  149.                             if(Translate_CR_LF)
  150.                                 Length = (* Translate_CR_LF)(ReadBuffer,Length);
  151.  
  152.                             if(Length)
  153.                             {
  154.                                 ConProcess(ReadBuffer,Length);
  155.  
  156.                                 Status = STATUS_READY;
  157.  
  158.                                 if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
  159.                                 {
  160.                                     if(!CheckIO(TimeRequest))
  161.                                         AbortIO(TimeRequest);
  162.  
  163.                                     WaitIO(TimeRequest);
  164.  
  165.                                     RestartSerial(FALSE);
  166.  
  167.                                     return(TRUE);
  168.                                 }
  169.                             }
  170.                         }
  171.                     }
  172.                 }
  173.                 while(Length);
  174.             }
  175.  
  176.             RestartSerial(FALSE);
  177.         }
  178.  
  179.         if(Signals & SIG_TIMER)
  180.         {
  181.             WaitIO(TimeRequest);
  182.  
  183.             return(FALSE);
  184.         }
  185.  
  186.         Signals = Wait(SIG_SERIAL | SIG_TIMER);
  187.     }
  188. }
  189.  
  190.     /* SendLinePrompt(STRPTR Line,LONG Len):
  191.      *
  192.      *    Send text line, wait for prompt.
  193.      */
  194.  
  195. BYTE
  196. SendLinePrompt(STRPTR Line,LONG Len)
  197. {
  198.     LONG i;
  199.  
  200.     if(Len == -1)
  201.         Len = strlen(Line);
  202.  
  203.     while(Len)
  204.     {
  205.         i = 0;
  206.  
  207.         while(i < Len && Line[i] != '\r')
  208.             i++;
  209.  
  210.  
  211.         if(Line[i] == '\r')
  212.         {
  213.             i++;
  214.  
  215.             SerWrite(Line,i);
  216.  
  217.             if(!WaitForPrompt(SendPrompt,SendPromptLen))
  218.                 return(FALSE);
  219.         }
  220.         else
  221.         {
  222.             if(i)
  223.                 SerWrite(Line,i);
  224.         }
  225.  
  226.         Len    -= i;
  227.         Line    += i;
  228.     }
  229.  
  230.     return(TRUE);
  231. }
  232.  
  233.     /* SendLineSimple(STRPTR Line,LONG Len):
  234.      *
  235.      *    Send a text line, no fancy features.
  236.      */
  237.  
  238. BYTE
  239. SendLineSimple(STRPTR Line,LONG Len)
  240. {
  241.     if(Len == -1)
  242.         Len = strlen(Line);
  243.  
  244.     SerWrite(Line,Len);
  245.  
  246.     return(TRUE);
  247. }
  248.  
  249.     /* SendLineDelay(STRPTR Line,LONG Len):
  250.      *
  251.      *    Send a text line, include a delay where necessary.
  252.      */
  253.  
  254. BYTE
  255. SendLineDelay(STRPTR Line,LONG Len)
  256. {
  257.     if(Len == -1)
  258.         Len = strlen(Line);
  259.  
  260.     while(Len--)
  261.     {
  262.         SerWrite(Line,1);
  263.  
  264.         if(*Line == '\r')
  265.         {
  266.             if(Config -> ClipConfig -> LineDelay)
  267.                 WaitTime(Config -> ClipConfig -> LineDelay / 100,(Config -> ClipConfig -> LineDelay % 100) * 10000);
  268.         }
  269.         else
  270.         {
  271.             if(Config -> ClipConfig -> CharDelay)
  272.                 WaitTime(Config -> ClipConfig -> CharDelay / 100,(Config -> ClipConfig -> CharDelay % 100) * 10000);
  273.         }
  274.  
  275.         Line++;
  276.     }
  277.  
  278.     return(TRUE);
  279. }
  280.  
  281.     /* SendLineEcho(STRPTR Line,LONG Len):
  282.      *
  283.      *    Send a text line, wait for single characters to be echoed.
  284.      */
  285.  
  286. BYTE
  287. SendLineEcho(STRPTR Line,LONG Len)
  288. {
  289.     ULONG Signals;
  290.  
  291.     if(DataHold)
  292.     {
  293.         DataHold = NULL;
  294.  
  295.         RestartSerial(FALSE);
  296.     }
  297.  
  298.     if(Len == -1)
  299.         Len = strlen(Line);
  300.  
  301.     while(Len--)
  302.     {
  303.         SerWrite(Line,1);
  304.  
  305.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  306.  
  307.         FOREVER
  308.         {
  309.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  310.  
  311.             if(Signals & SIG_SERIAL)
  312.             {
  313.                 if(!WaitSerialRead())
  314.                 {
  315.                     LONG Length;
  316.  
  317.                     if(Translate_CR_LF)
  318.                         Length = (* Translate_CR_LF)(ReadBuffer,1);
  319.                     else
  320.                         Length = 1;
  321.  
  322.                     if(Length > Len)
  323.                         Length = Len;
  324.  
  325.                     if(Length)
  326.                     {
  327.                         if(!memcmp(ReadBuffer,Line,Length))
  328.                         {
  329.                             if(!CheckIO(TimeRequest))
  330.                                 AbortIO(TimeRequest);
  331.  
  332.                             WaitIO(TimeRequest);
  333.  
  334.                             RestartSerial(FALSE);
  335.  
  336.                             break;
  337.                         }
  338.                     }
  339.  
  340.                     RestartSerial(FALSE);
  341.                 }
  342.                 else
  343.                 {
  344.                     if(!CheckIO(TimeRequest))
  345.                         AbortIO(TimeRequest);
  346.  
  347.                     WaitIO(TimeRequest);
  348.  
  349.                     RestartSerial(FALSE);
  350.  
  351.                     return(FALSE);
  352.                 }
  353.             }
  354.  
  355.             if(Signals & SIG_TIMER)
  356.             {
  357.                 WaitIO(TimeRequest);
  358.  
  359.                 return(FALSE);
  360.             }
  361.         }
  362.  
  363.         Line++;
  364.     }
  365.  
  366.     return(TRUE);
  367. }
  368.  
  369.     /* SendLineAnyEcho(STRPTR Line,LONG Len):
  370.      *
  371.      *    Send a text line, wait for characters to be echoed.
  372.      */
  373.  
  374. BYTE
  375. SendLineAnyEcho(STRPTR Line,LONG Len)
  376. {
  377.     ULONG Signals;
  378.  
  379.     if(DataHold)
  380.     {
  381.         DataHold = NULL;
  382.  
  383.         RestartSerial(FALSE);
  384.     }
  385.  
  386.     if(Len == -1)
  387.         Len = strlen(Line);
  388.  
  389.     while(Len--)
  390.     {
  391.         SerWrite(Line,1);
  392.  
  393.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  394.  
  395.         FOREVER
  396.         {
  397.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  398.  
  399.             if(Signals & SIG_SERIAL)
  400.             {
  401.                 if(!WaitSerialRead())
  402.                 {
  403.                     if(!CheckIO(TimeRequest))
  404.                         AbortIO(TimeRequest);
  405.  
  406.                     WaitIO(TimeRequest);
  407.  
  408.                     RestartSerial(FALSE);
  409.  
  410.                     break;
  411.                 }
  412.                 else
  413.                 {
  414.                     if(!CheckIO(TimeRequest))
  415.                         AbortIO(TimeRequest);
  416.  
  417.                     WaitIO(TimeRequest);
  418.  
  419.                     RestartSerial(FALSE);
  420.  
  421.                     return(FALSE);
  422.                 }
  423.             }
  424.  
  425.             if(Signals & SIG_TIMER)
  426.             {
  427.                 WaitIO(TimeRequest);
  428.  
  429.                 return(FALSE);
  430.             }
  431.         }
  432.  
  433.         Line++;
  434.     }
  435.  
  436.     return(TRUE);
  437. }
  438.  
  439.     /* SendLineKeyDelay(STRPTR Line,LONG Len):
  440.      *
  441.      *    Send a text line, include keyboard delay pauses between characters.
  442.      */
  443.  
  444. BYTE
  445. SendLineKeyDelay(STRPTR Line,LONG Len)
  446. {
  447.     struct Preferences Prefs;
  448.  
  449.     if(Len == -1)
  450.         Len = strlen(Line);
  451.  
  452.         /* Get current key repeat delay. */
  453.  
  454.     GetPrefs(&Prefs,offsetof(struct Preferences,KeyRptDelay));
  455.  
  456.         /* Any delay specified at all? */
  457.  
  458.     if(Prefs . KeyRptSpeed . tv_secs || Prefs . KeyRptSpeed . tv_micro)
  459.     {
  460.         while(Len--)
  461.         {
  462.             SerWrite(Line++,1);
  463.  
  464.             if(Len)
  465.             {
  466.                 TimeRequest -> tr_node . io_Command    = TR_ADDREQUEST;
  467.                 TimeRequest -> tr_time            = Prefs . KeyRptSpeed;
  468.  
  469.                 DoIO(TimeRequest);
  470.             }
  471.         }
  472.     }
  473.     else
  474.         SerWrite(Line,Len);
  475.  
  476.     return(TRUE);
  477. }
  478.  
  479.     /* SendSetup():
  480.      *
  481.      *    Choose the right routine for the text line output job.
  482.      */
  483.  
  484. VOID
  485. SendSetup()
  486. {
  487.         /* Prepare the prompt string. */
  488.  
  489.     if(Config -> ClipConfig -> LinePrompt[0])
  490.         SendPromptLen = TranslateString(Config -> ClipConfig -> LinePrompt,SendPrompt);
  491.     else
  492.     {
  493.         SendPrompt[0] = 0;
  494.         SendPromptLen = 0;
  495.     }
  496.  
  497.         /* Pick the line send routine. */
  498.  
  499.     switch(Config -> ClipConfig -> PacingMode)
  500.     {
  501.         case PACE_DIRECT:
  502.  
  503.             SendLine = SendLineSimple;
  504.             break;
  505.  
  506.         case PACE_ECHO:
  507.  
  508.             if(Config -> ClipConfig -> SendTimeout)
  509.                 SendLine = SendLineEcho;
  510.             else
  511.                 SendLine = SendLineSimple;
  512.  
  513.             break;
  514.  
  515.         case PACE_ANYECHO:
  516.  
  517.             if(Config -> ClipConfig -> SendTimeout)
  518.                 SendLine = SendLineAnyEcho;
  519.             else
  520.                 SendLine = SendLineSimple;
  521.  
  522.             break;
  523.  
  524.         case PACE_PROMPT:
  525.  
  526.             if(Config -> ClipConfig -> SendTimeout)
  527.                 SendLine = SendLinePrompt;
  528.             else
  529.                 SendLine = SendLineSimple;
  530.  
  531.             break;
  532.  
  533.         case PACE_DELAY:
  534.  
  535.             if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
  536.                 SendLine = SendLineDelay;
  537.             else
  538.                 SendLine = SendLineSimple;
  539.  
  540.             break;
  541.  
  542.         case PACE_KEYBOARD:
  543.  
  544.             SendLine = SendLineKeyDelay;
  545.             break;
  546.     }
  547. }
  548.